﻿using black_screen_mode_server.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace black_screen_mode_server.Pages
{
    /// <summary>
    /// Interaction logic for ListOperationForm.xaml
    /// </summary>
    public partial class ListOperationForm : Window
    {
        public enum operationType
        {
            Add,
            Edit
        }


        public ListOperationForm(operationType type, ListInfo lstInfo, int listType)
        {
            Logger.Singleton.Info($"ListOperationForm--type:{type},listType:{listType}");
            InitializeComponent();
            this.txtCategory.ItemsSource = CategoryConstant.categoryStrMap.Keys;
            this.listOperInfo.Id = lstInfo.Id;
            this.listOperInfo.GroupName = lstInfo.GroupName;
            this.listOperInfo.AreaName = lstInfo.AreaName;
            this.listOperInfo.Category = lstInfo.Category;
            this.listOperInfo.Block = lstInfo.Block;
            this.listOperInfo.Source = lstInfo.Source;
            this.listOperInfo.EventType = lstInfo.EventType;
            if (type == operationType.Add)
            {
                this.Title = "添加";
                this.btnSave.Click += (sender, e) =>
                {
                    try
                    {
                        ListInfoSqlHelper.AddListInfo(this.listOperInfo, listType);
                        this.Close();
                    }
                    catch (Exception)
                    {

                        MessageBox.Show("请填写正确的信息！");
                    }
                  
                };
            }
            else
            {
                this.Title = "编辑";
                //编辑的时候，ID不能更改
                this.txtId.IsEnabled = false;
             
                this.btnSave.Click += (sender, e) =>
                {
                    try
                    {
                        ListInfoSqlHelper.UpdateListInfo(lstInfo.Id, this.listOperInfo, listType);
                        this.Close();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("请填写正确的信息");
                    }

                  
                };
            }

          

        }
        public ListOperationForm()
        {
            InitializeComponent();
            this.txtCategory.ItemsSource = CategoryConstant.categoryStrMap.Keys;
        }


        public bool Verify()
        {
            if(string.IsNullOrEmpty(txtId.Text  ) || string.IsNullOrEmpty(txtGroupName.Text) || string.IsNullOrEmpty(txtAreaName.Text) || string.IsNullOrEmpty(txtEventType.Text) || string.IsNullOrEmpty(txtBlock.Text) || string.IsNullOrEmpty(txtSource.Text) || string.IsNullOrEmpty(txtCategory.Text))
            {
                MessageBox.Show("请填写完整信息！");
                return false;
            }
            if(string.Equals(txtCategory.Text, "未知类型" ))
            {
                MessageBox.Show("请选择正确的类别！");
                return false;
            }
            //txtId的值必须为数字
            if (!int.TryParse(txtId.Text, out int id))
            {
                MessageBox.Show("请修改Id为数字！");
                return false;
            }

            return true;
        }

    }
}
